/*
 * @Author: ZephyrGao
 * @Date: 2023-11-01 13:43:58
 * @Description: 
 * Copyright (c) 2023 by ZephyrGao, All Rights Reserved. 
 */
// import 'package:cn_flutter_ui/components/base/cn_base_button.dart';

import 'dart:developer';

import 'package:cn_flutter_ui/src/constants/vant4_constant.dart';
import 'package:cn_flutter_ui/vant4.dart';
import 'package:flutter/material.dart';

extension ButtonTypeExtension on ButtonType {
  Color get backgroundColor => [
        Vant4CommonConstant.primary,
        Vant4CommonConstant.success,
        Vant4CommonConstant.warning,
        Vant4CommonConstant.danger,
        Vant4CommonConstant.white,
      ][index];
  Color get textColor => [
        Vant4CommonConstant.white,
        Vant4CommonConstant.white,
        Vant4CommonConstant.white,
        Vant4CommonConstant.white,
        Vant4CommonConstant.textN8,
      ][index];
}

enum ButtonType {
  primary,
  success,
  warning,
  danger,
  defaults,
}

enum ButtonSize { large, normal, small, mini }

extension ButtonSizeExtension on ButtonSize {
  double get height => [
        Vant4ButtonConstant.largeHeight,
        Vant4ButtonConstant.normalHeight,
        Vant4ButtonConstant.smallHeight,
        Vant4ButtonConstant.miniHeight,
      ][index];
  EdgeInsets get padding => [
        Vant4ButtonConstant.largePadding,
        Vant4ButtonConstant.normalPadding,
        Vant4ButtonConstant.smallPadding,
        Vant4ButtonConstant.miniPadding,
      ][index];
  double get radius => [
        Vant4ButtonConstant.vanNormalRadis,
        Vant4ButtonConstant.vanNormalRadis,
        Vant4ButtonConstant.vanNormalRadis,
        Vant4ButtonConstant.vanNormalRadis,
      ][index];
  double get fontSize => [
        Vant4ButtonConstant.vanLargeFontSize,
        Vant4ButtonConstant.vanNormalFontSize,
        Vant4ButtonConstant.vanSmallFontSize,
        Vant4ButtonConstant.vanMiniFontSize
      ][index];
}

enum ButtonShape { round, square }

class VanButton extends StatefulWidget {
  const VanButton({
    super.key,
    this.buttonType = ButtonType.defaults,
    this.buttonSize = ButtonSize.normal,
    this.width,
    this.height,
    this.text,
    this.textStyle,
    this.color,
    this.gradient,
    this.icon,
    this.block,
    this.plain = false,
    this.disabled = false,
    this.hairline = false,
    this.loading = false,
    this.round = false,
    this.loadingText,
    this.loadingSize = 20,
    this.loadingWidget,
    this.radius,
    this.onTap,
  })  : assert(icon == null || icon is VanIconData || icon is VanIcon,
            'content must be null, String, or VanIcon'),
        assert((color == null || gradient == null),
            'color and gradient are mutually exclusive');

  /// 可选值为 `primary` `success` `warning` `danger`,默认值 `defaults`
  final ButtonType buttonType;

  /// 可选值为 `large` `normal` `small` `mini`,默认值 `normal`
  final ButtonSize buttonSize;

  /// 按钮宽 设置此属性后ButtonSize失效
  final double? width;

  /// 按钮高 设置此属性后ButtonSize失效
  final double? height;

  /// 按钮文字
  final String? text;

  /// 按钮文字
  final TextStyle? textStyle;

  /// 按钮颜色,支持传入渐变色
  final Color? color;

  /// 按钮颜色,支持传入渐变色
  /// ```
  ///   LinearGradient(
  ///     colors: [Colors.red, Colors.blue],
  ///        begin: Alignment.topLeft,
  ///        end: Alignment.bottomRight,
  ///     ),
  ///
  /// ```

  final LinearGradient? gradient;

  /// 左侧图标名称或图片链接，等同于 Icon 组件的 name 属性
  final dynamic icon;

  /// 是否为块级元素
  final bool? block;

  /// 是否为朴素按钮
  final bool plain;

  /// 是否为圆形按钮 round square
  final bool round;

  /// 是否禁用按钮
  final bool disabled;

  /// 是否使用 0.5px 边框
  final bool hairline;

  /// radius
  final double? radius;

  /// 是否显示为加载状态
  final bool loading;

  /// 加载状态提示文字 默认van-icon
  final String? loadingText;

  /// 加载图标大小 默认van-icon
  final double loadingSize;

  /// 自定义loadingWidget
  final Widget? loadingWidget;

  final GestureTapCallback? onTap;

  @override
  State<VanButton> createState() => _VantButtonState();
}

class _VantButtonState extends State<VanButton> {
  dynamic iconWidget;
  @override
  void initState() {
    super.initState();
    if (widget.icon is VanIconData) {
      iconWidget = VanIcon(
        widget.icon,
        color: widget.buttonType.textColor,
        size: widget.buttonSize.fontSize * 1.2,
      );
    }
    if (widget.icon is VanIcon) {
      iconWidget = widget.icon;
    }
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: widget.onTap,
      // splashColor: Colors.black,
      // highlightColor: Colors.red, // 设置点击时的高亮颜色
      // borderRadius: BorderRadius.circular(10), // 设置水波纹的形状为圆角矩形
      // radius: 220, // 设置水波纹的扩散范围
      // splashFactory: InkRipple.splashFactory, // 使用默认的水波纹动画效果
      highlightColor: widget.disabled ? Colors.transparent : null,
      splashColor: widget.disabled ? Colors.transparent : null,
      child: Ink(
          padding: widget.buttonSize.padding,
          width: widget.width,
          height: widget.height ?? widget.buttonSize.height,
          decoration: BoxDecoration(
              gradient: widget.gradient,
              color: widget.gradient == null
                  ? (widget.buttonType == ButtonType.defaults
                      ? Colors.white.withOpacity(widget.disabled ? 0.5 : 1)
                      : widget.plain
                          ? Colors.white.withOpacity(widget.disabled ? 0.5 : 1)
                          : widget.buttonType.backgroundColor
                              .withOpacity(widget.disabled ? 0.5 : 1))
                  : null,
              borderRadius: widget.round
                  ? BorderRadius.circular(Vant4ButtonConstant.vanMaxRadis)
                  : (BorderRadius.circular(
                      widget.radius ?? widget.buttonSize.radius)),
              border: widget.buttonType == ButtonType.defaults
                  ? Border.all(
                      width: widget.hairline ? 0.5 : 1,
                      color: Vant4CommonConstant.vanGray4
                          .withOpacity(widget.disabled ? 0.5 : 1))
                  : widget.plain
                      ? Border.all(
                          width: widget.hairline ? 0.5 : 1,
                          color: widget.buttonType.backgroundColor
                              .withOpacity(widget.disabled ? 0.5 : 1))
                      : null),
          child:
              !widget.loading ? _defaultIconWidget() : _defaultloadingWidget()),
    );
  }

  Widget _defaultloadingWidget() {
    return widget.loadingWidget ??
        VanLoading(
          type: VanLoadingType.circular,
          color: widget.plain
              ? widget.buttonType.backgroundColor
                  .withOpacity(widget.disabled ? 0.5 : 1)
              : widget.buttonType.textColor
                  .withOpacity(widget.disabled ? 0.5 : 1),
          size: widget.loadingSize,
          strokeWidth: 1,
          text: widget.loadingText,
          textColor: widget.plain
              ? widget.buttonType.backgroundColor
                  .withOpacity(widget.disabled ? 0.5 : 1)
              : Colors.white.withOpacity(widget.disabled ? 0.5 : 1),
          textSize: widget.buttonSize.fontSize,
        );
  }

  Widget _defaultIconWidget() {
    return iconWidget ??
        Container(
            alignment: Alignment.center,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                iconWidget ?? Vant4CommonConstant.empty,
                iconWidget != null && widget.text != null
                    ? Vant4CommonConstant.baseMarginLeft
                    : Vant4CommonConstant.empty,
                Text(
                  widget.text ?? "",
                  style: widget.textStyle ??
                      TextStyle(
                          fontSize: widget.buttonSize.fontSize,
                          color: widget.buttonType == ButtonType.defaults
                              ? Vant4CommonConstant.vanGray8
                                  .withOpacity(widget.disabled ? 0.5 : 1)
                              : widget.plain
                                  ? widget.buttonType.backgroundColor
                                      .withOpacity(widget.disabled ? 0.5 : 1)
                                  : Colors.white
                                      .withOpacity(widget.disabled ? 0.5 : 1),
                          fontWeight: FontWeight.bold),
                )
              ],
            )
            // width: widget,
            );
  }
}
